const api = require('../../../api/mop_api');
import { toast, log, err, to } from '../../../utils/wxapi';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    size: 10,
    list: [],
    pageTotal: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.paylist();
  },
  async paylist() {
    try {
      const { page, size } = this.data;
      const res = await api.payhdList({ page, size });
      if (res.respCode === 200) {
        const list = JSON.parse(res.source);
        console.log(list);
        if (this.data.list.length === 0) {
          this.setData({ list, pageTotal: res.pageTotal });
        } else {
          this.setData({
            list: this.data.list.concat(list),
            pageTotal: res.pageTotal,
          });
        }
      } else {
        toast(res.msg);
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  enit(e) {
    const id = e.currentTarget.dataset.item.id;
    to('/dj/pages/addpayhd/addpayhd?id=' + id);
  },
  toadd() {
    to('/dj/pages/addpayhd/addpayhd');
  },
  async putsta(e) {
    try {
      const { id, status } = e.currentTarget.dataset;
      const newStatus = status === 'DISABLED' ? 'NORMAL' : 'DISABLED';
      const data = {
        id,
        status: newStatus,
      };
      const res = await api.paystaput(data);
      if (res.respCode === 200) {
        this.setData({
          page: 1,
          list: [],
        });
        this.paylist();
        toast('修改成功');
      } else {
        toast(res.msg);
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  delhuodong(e) {
    try {
      const { id } = e.currentTarget.dataset;
      wx.showModal({
        title: '提示',
        content: '确定删除此活动吗？',
        complete: async res => {
          if (res.confirm) {
            const res = await api.payhdDel({ id });
            log(res);
            if (res.respCode === 200) {
              this.setData({
                page: 1,
                list: [],
              });
              this.paylist();
              toast('删除成功');
            } else {
              toast(res.msg);
            }
          }
        },
      });
    } catch (error) {
      err(error);
      throw error;
    }
  },
  onHide() {
    this.setData({
      list: [],
      page: 1,
    });
  },
});
